package cn.gzsxt.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import cn.gzsxt.interceptor.AdminSessionUpdateInterceptor;
import cn.gzsxt.interceptor.LoginStatusInterceptor;
import cn.gzsxt.interceptor.PermissionHandlerInterceptor;
import cn.gzsxt.interceptor.TokenFormInterceptor;

@Configuration
@EnableWebMvc
public class MvcConfig extends WebMvcConfigurerAdapter {

	/**
	 * 注意事项：必须要在Spring容器里面的对象才可以使用依赖注入
	 * 因为该拦截器需要通过依赖注入获得容器中的AdminService的对象，所以需要通过@Bean注入到容器里面
	 * 
	 */
	@Bean
	public AdminSessionUpdateInterceptor getAdminSessionUpdateInterceptor() {
		return new AdminSessionUpdateInterceptor();
	}

	// 1.打开静态资源访问 <mvc:default-servlet-hanlder>
	@Override
	public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
		configurer.enable();
	}

	// 2.注解拦截器配置<mvc:interceptors>
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		// 创建拦截器对象-登录状态
		LoginStatusInterceptor loginStatusInterceptor = new LoginStatusInterceptor();
		// 将管理员状态拦截器加入到容器
		InterceptorRegistration loginStatusRegistration = registry.addInterceptor(loginStatusInterceptor);
		// 设置拦截器的规则,拦截所有的请求
		loginStatusRegistration.addPathPatterns("/**");
		// 排除不拦截的请求-登录不拦截
		loginStatusRegistration.excludePathPatterns("/admin/loginAdmin");

		// 创建拦截器对象-权限验证
		PermissionHandlerInterceptor permissionHandlerInterceptor = new PermissionHandlerInterceptor();
		InterceptorRegistration permissionHandlerRegistration = registry.addInterceptor(permissionHandlerInterceptor);
		permissionHandlerRegistration.addPathPatterns("/**");
		// 排除登录注销不拦截
		permissionHandlerRegistration.excludePathPatterns("/admin/loginAdmin");
		permissionHandlerRegistration.excludePathPatterns("/admin/undoAdmin");

		// 配置防重提交拦截器
		TokenFormInterceptor tokenFormInterceptor = new TokenFormInterceptor();
		InterceptorRegistration tokenFormRegistration = registry.addInterceptor(tokenFormInterceptor);
		tokenFormRegistration.addPathPatterns("/**");

		// 及时更新修改信息，更新当前用户

		InterceptorRegistration registration = registry.addInterceptor(this.getAdminSessionUpdateInterceptor());
		registration.addPathPatterns("/**");

	}

	@Bean
	public InternalResourceViewResolver getViewResolver() {
		InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
		// 1.配置前缀
		viewResolver.setPrefix("/WEB-INF/views/");
		viewResolver.setSuffix(".jsp");
		return viewResolver;
	}

}
